﻿#include <opencv2/opencv.hpp>
#include <QBuffer>
#include "JZModuleModel.h"
#include "JZNodeBind.h"
#include "JZScriptEnvironment.h"
#include "JZModelNode.h"
#include "JZNodeFactory.h"
#include "JZContainer.h"
#include "Yolo/JZYolo.h"
#include "Yolo/JZYoloView.h"
#include "PaddleOCR/JZPaddleOCR.h"
#include "../JZModuleConfigFactory.h"

using namespace cv;

//JZModuleModel
JZModuleModel::JZModuleModel()
{        
    m_name = "model";

    auto inst = JZModuleConfigFactory<JZModelConfig>::instance();
    inst->regist(Model_Yolo, JZModuleConfigCreator<JZModelYoloConfig>);
}

JZModuleModel::~JZModuleModel()
{
}

void JZModuleModel::regist(JZScriptEnvironment *env)
{    
    auto func_inst = env->functionManager();
    int cls_id = Module_ModelType;

    jzbind::ClassBind<JZModel> cls_model(cls_id++, "JZModel");
    cls_model.def("init", true, &JZModel::init);
    cls_model.regist();

    jzbind::ClassBind<JZModelManager> cls_model_manger(cls_id++, "JZModelManager");
    cls_model_manger.regist();

    jzbind::ClassBind<JZYoloResult> cls_yolo_ret(cls_id++, "JZYoloResult");
    registList<JZYoloResult>(env, cls_id++);

    cls_yolo_ret.def("toGraphics", true, &JZYoloResult::toGraphics);
    cls_yolo_ret.regist();

    jzbind::ClassBind<JZYolo> cls_yolo(cls_id++, "JZYolo", "JZModel");
    cls_yolo.def("forward", true, &JZYolo::forward);
    cls_yolo.regist();

    //ocr
    jzbind::ClassBind<JZOCRResult> cls_ocr_ret(cls_id++, "JZOCRResult");
    registList<JZOCRResult>(env, cls_id++);

    cls_ocr_ret.def("toGraphics", true, &JZOCRResult::toGraphics);
    cls_ocr_ret.regist();

    jzbind::ClassBind<JZPaddleOCR> cls_ocr(cls_id++, "JZPaddleOCR", "QObject");
    cls_ocr.def("init", true, &JZPaddleOCR::init);
    cls_ocr.def("ocr", true, &JZPaddleOCR::ocr);
    cls_ocr.regist();

    int model_ptr_id = JZNodeType::pointerType(cls_model.id());
    int yolo_ptr_id = JZNodeType::pointerType(cls_yolo.id());
    env->registConvertExplicitly(model_ptr_id, yolo_ptr_id, JZObjectCastDown<JZYolo,JZModel>);

    jzbind::ClassBind<JZYoloView> cls_yolo_view(cls_id++, "JZYoloView", "QWidget");
    cls_yolo_view.def("setYoloResult", true, &JZYoloView::setYoloResult);
    cls_yolo_view.regist();
    
    func_inst->registCFunction("JZModelInit", true, jzbind::createFuncion(JZModelInit));
    func_inst->registCFunction("JZModelGet", false, jzbind::createFuncion(JZModelGet, CFunction::Reference));
    func_inst->registCFunction("JZYoloForward", true, jzbind::createFuncion(JZYoloForward));

    env->nodeFactory()->registNode(Node_ModelInit, createJZNode<JZNodeModelInit>);
    env->nodeFactory()->registNode(Node_ModelForward, createJZNode<JZNodeModelForward>);
    env->nodeFactory()->registNode(Node_ModelYolo, createJZNode<JZNodeModelYolo>);
}

void JZModuleModel::unregist(JZScriptEnvironment *env)
{
}